package com.example.zhangxin.myhuatuob2b.fragment.home.store;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zhangxin.myhuatuob2b.MyApplication;
import com.example.zhangxin.myhuatuob2b.R;
import com.example.zhangxin.myhuatuob2b.utils.PreferenceUtils;
import com.example.zhangxin.myhuatuob2b.utils.URL;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * A simple {@link Fragment} subclass.
 * <string name="username">wolaile</string>
 <string name="key">b663b2b037c44272def620611becfd1b</string>
 <boolean name="is_frist" value="false" />
 <string name="userid">1644</string>
 */
public class UserNameRegisterFragment extends Fragment implements View.OnClickListener {


    private EditText editUserName;
    private LinearLayout ly_edit_name;
    private EditText editPassword;
    private EditText editPasswordConfirm;
    private EditText editEmail;
    private TextView buttonSend;

    public UserNameRegisterFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_user_name_register, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        editUserName = (EditText) view.findViewById(R.id.editUserName);
        editPassword = (EditText) view.findViewById(R.id.editPassword);
        editEmail = (EditText) view.findViewById(R.id.editEmail);
        editPasswordConfirm = (EditText) view.findViewById(R.id.editPasswordConfirm);
        buttonSend = (TextView) view.findViewById(R.id.buttonSend);

        buttonSend.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String username = editUserName.getText().toString();
        String password = editPassword.getText().toString();
        String password_confirm = editPasswordConfirm.getText().toString();
        String email = editEmail.getText().toString();
        if (username.equals("") || username == null) {
            Toast.makeText(getActivity(), "用户名不能为空", Toast.LENGTH_SHORT).show();
            ;
            return;
        }
        if (password.equals("") || password == null) {
            Toast.makeText(getActivity(), "密码不能为空", Toast.LENGTH_SHORT).show();
            ;
            return;
        }
        if (password_confirm.equals("") || password_confirm == null) {
            Toast.makeText(getActivity(), "确认密码不能为空", Toast.LENGTH_SHORT).show();
            ;
            return;
        }
        if (!password.equals(password_confirm)) {
            Toast.makeText(getActivity(), "两次密码不同", Toast.LENGTH_SHORT).show();
            ;
            return;
        }
        if (email.equals("") || email == null) {
            Toast.makeText(getActivity(), "邮箱不能为空", Toast.LENGTH_SHORT).show();
            ;
            return;
        }
        SendData(username, password, password_confirm, email);
    }

    private void SendData(String username, String password, String password_confirm, String email) {

        String url = URL.URL_REGISTER;

        RequestParams params = new RequestParams(url);
        params.addBodyParameter("username", username);
        params.addBodyParameter("password", password);
        params.addBodyParameter("password_confirm", password_confirm);
        params.addBodyParameter("email", email);
        params.addBodyParameter("client", "android");
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject object = new JSONObject(result);
                    if(!object.isNull("datas")){
                        String datas = object.getString("datas");
                        JSONObject obj = new JSONObject(datas);
                        String member_role = obj.optString("member_role");
                        String key = obj.optString("key");
                        String userid = obj.optString("userid");
                        String username = obj.optString("username");


                        PreferenceUtils.setString(MyApplication.getAppcation(),"key",key);
                        PreferenceUtils.setString(MyApplication.getAppcation(),"userid",userid);
                        PreferenceUtils.setString(MyApplication.getAppcation(),"username",username);
                        PreferenceUtils.setString(MyApplication.getAppcation(),"member_role",member_role);

                        getActivity().finish();
                    }else{
                        Toast.makeText(MyApplication.getAppcation(),"错误信息",Toast.LENGTH_SHORT).show();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
